﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using AmazonGlacierGUI.Core;
using AmazonGlacierGUI.Core.Helpers.AmazonGlacier;
using AmazonGlacierGUI.Core.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure;
using AmazonGlacierGUI.UI.Infrastructure.MVVMFramework;
using GalaSoft.MvvmLight.Command;

namespace AmazonGlacierGUI.UI.ViewModels
{
    //15-Sep-2012 Priyan R
    public class CreateNewVaultViewModel:AppViewModelBase
    {

        public CreateNewVaultViewModel()
        {
            WireCommands();
        }
        public override string OnClose()
        {
            VaultName = null;
            return null;
        }
        protected override void OnCloseCommand()
        {
            OnClose();
            Close();
        }
        
        private void OnCreateCommand()
        {
            if(!ValidateAll()) return;
            var helper = new AmazonGlacierHelper();
            helper.AccessKey = AppContext.Current.Settings.AccessKey;
            helper.SecretAccessKey = AppContext.Current.Settings.SecretAccessKey;
            helper.Region = Region.Value;
            try
            {
                helper.CreateNewVault(VaultName);
                Close();                                    
            }
            catch (Exception ex)
            {
                UIHelper.ShowError(AppUtility.GetExceptionMessage(ex),()=> { });                                
                return;
            }
        }

        void WireCommands()
        {
            CreateCommand=new RelayCommand(OnCreateCommand);
        }
        
        public RelayCommand CreateCommand { get; set; }
        private AmazonRegionEnum? _region;
        [Required]
        public AmazonRegionEnum? Region
        {
            get { return _region; }
            set
            {
                if (_region != value)
                {
                    _region = value;
                    RaisePropertyChanged(() => Region);
                }
            }
        }

        private string _vaultName;
        [Required]
        public string VaultName
        {
            get { return _vaultName; }
            set
            {
                if (_vaultName != value)
                {
                    _vaultName = value;         
                    RaisePropertyChanged(() => VaultName);
                }
            }
        }
        
    }
}
